package com.example.homework.ui.main

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import com.example.homework.R
import com.example.homework.databinding.FragmentResultBinding

private const val ARG_RESULT_TEXT = "resultText"

class ResultFragment : Fragment() {
    private var resultText: String? = null
    private var _binding: FragmentResultBinding? = null
    private val binding: FragmentResultBinding
        get() = _binding!!

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            resultText = it.getString(ARG_RESULT_TEXT)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentResultBinding.inflate(inflater, container, false)
        binding.resTextView.text = resultText
        binding.repeatButton.setOnClickListener {
            findNavController().navigate(R.id.action_resultFragment_to_mainFragment)
        }
        return binding.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    companion object {
        @JvmStatic
        fun newInstance(resultText: String) =
            ResultFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_RESULT_TEXT, resultText)
                }
            }
    }
}